/**
 * 
 */
package com.cqan.account.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cqan.entity.pk.IdEntity;


/**
 * @Description TODO
 * @author slaton
 * @date 2012-4-27下午6:21:54
 *
 */
@SuppressWarnings("serial")
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "DMS_RESOURCE")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Resource extends IdEntity {

	public enum ResourceType{
		GENERAL(1),MENU(0);
		
		int value;
		
		ResourceType(Integer value){
			this.value = value;
		}
		
		private Integer getValue(){
			return this.value;
		}
		
		public String getLabel(){
			switch (this) {
			case MENU:
				return "菜单";
			case GENERAL:
				return "普通";
			}
			return "其它";
		}
		
		
		public static ResourceType getTypeByValue(Integer value){
			
			for (ResourceType v : ResourceType.values()) {
				if (v.getValue()==value) {
					return v;
				}
			}
			return null;
		}
	}
	
	private List<Resource> children;
	
	private Boolean enabled;
	
	private String icon;
	
	private String name;
	
	private Resource parent;
	
	private ResourceType type;
	
	private String url;
	
	private Long createTime;
	
	
	@Column(name="CREATE_TIME")
	public Long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Long createTime) {
		this.createTime = createTime;
	}

	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REFRESH}, fetch = FetchType.EAGER, targetEntity = Resource.class)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Resource> getChildren() {
		return children;
	}

	@Column(name="ICON")
	public String getIcon() {
		return icon;
	}

	@Column(name="NAME")
	public String getName() {
		return name;
	}
	@ManyToOne
	@JoinColumn(name="PID")
	public Resource getParent() {
		return parent;
	}

	@Column(name="TYPE")
	@Enumerated(EnumType.STRING)
	public ResourceType getType() {
		return type;
	}

	@Column(name="URL")
	public String getUrl() {
		return url;
	}

	@Column(name="ENABLED")
	public Boolean getEnabled() {
		return enabled;
	}

	public void setChildren(List<Resource> children) {
		this.children = children;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParent(Resource parent) {
		this.parent = parent;
	}

	public void setType(ResourceType type) {
		this.type = type;
	}
	
	
	public void setUrl(String url) {
		this.url = url;
	}
	
}
